package com.demo.phone.web;

import com.baomidou.mybatisplus.plugins.Page;
import com.demo.phone.domain.Phone;
import com.demo.phone.service.PhoneService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


/**
 * @Description:
 * @author: wangshuai
 * @date: 2018/4/13 8:30
 */
@RestController
public class PhoneRestController {

    private final PhoneService phoneService;

    public PhoneRestController(PhoneService phoneService) {
        this.phoneService = phoneService;
    }

    @PostMapping("/api/public/creatPhone")
    public Integer creatPhone(){
       return phoneService.creatPhone();
    }
    @PutMapping("/api/updatePhone")
    public Integer updatePhone(@RequestBody Phone phone){
        return  phoneService.updatePhone(phone);
    }
    @GetMapping("/api/getPhone/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public Phone getPhone(@PathVariable("id") Integer id){
        return phoneService.getPhoneById(id);
    }
    @GetMapping("/api/public/api")
    public String publicAPI() {
        return "this is public API";
    }


    @GetMapping("/api/public/selectUserPage")
    public Object selectUserPage(Page<Phone> page) {
        return  phoneService.selectUserPage(page);
    }

    @PutMapping("/api/public/update")
    public Object updatePhoneById(Integer id){

        Phone phone = getPhone(id);

        phone.setProductPrice(1000D);
        if(phoneService.updateById(phone)){
            System.out.println("Update successfully");
        }else{
            System.out.println("Update failed due to modified by others");
        }
        return getPhone(id);
    }
}
